import random

# 初始化游戏地图，这里简单设定 20 个格子
MAP_SIZE = 20
# 初始资金
INITIAL_MONEY = 1500
# 租金系数
RENT_RATIO = 0.2

# 初始化地图格子信息
def initialize_map():
    game_map = []
    for i in range(MAP_SIZE):
        game_map.append({
            "name": f"Land {i + 1}",
            "owner": None,
            "price": random.randint(100, 300),
            "rent": 0
        })
    return game_map

# 初始化玩家信息
def initialize_players(num_players):
    players = []
    for i in range(num_players):
        players.append({
            "name": f"Player {i + 1}",
            "position": 0,
            "money": INITIAL_MONEY
        })
    return players

# 掷骰子
def roll_dice():
    return random.randint(1, 6)

# 移动玩家
def move_player(player, steps, game_map):
    new_position = (player["position"] + steps) % MAP_SIZE
    player["position"] = new_position
    current_land = game_map[new_position]
    print(f"{player['name']} moves to {current_land['name']}.")
    return player

# 处理玩家购买地产
def handle_purchase(player, land):
    if land["owner"] is None:
        if player["money"] >= land["price"]:
            choice = input(f"{land['name']} is available for purchase at ${land['price']}. Do you want to buy it? (y/n): ")
            if choice.lower() == 'y':
                player["money"] -= land["price"]
                land["owner"] = player["name"]
                land["rent"] = int(land["price"] * RENT_RATIO)
                print(f"{player['name']} buys {land['name']}. Remaining money: ${player['money']}")
        else:
            print(f"{player['name']} doesn't have enough money to buy {land['name']}.")
    elif land["owner"] != player["name"]:
        rent = land["rent"]
        if player["money"] >= rent:
            player["money"] -= rent
            for p in players:
                if p["name"] == land["owner"]:
                    p["money"] += rent
            print(f"{player['name']} pays ${rent} rent to {land['owner']}. Remaining money: ${player['money']}")
        else:
            print(f"{player['name']} doesn't have enough money to pay the rent. Game over for {player['name']}.")
            return False
    return True

# 检查是否有玩家破产
def check_bankruptcy(players):
    bankrupt_players = []
    for player in players:
        if player["money"] < 0:
            bankrupt_players.append(player)
    for bankrupt in bankrupt_players:
        players.remove(bankrupt)
    return len(players) > 1

# 主游戏循环
def main(num_players):
    game_map = initialize_map()
    global players
    players = initialize_players(num_players)
    current_player_index = 0

    while True:
        current_player = players[current_player_index]
        print(f"{current_player['name']}'s turn.")
        input("Press Enter to roll the dice...")
        dice_result = roll_dice()
        print(f"{current_player['name']} rolled a {dice_result}.")
        current_player = move_player(current_player, dice_result, game_map)
        current_land = game_map[current_player["position"]]
        if not handle_purchase(current_player, current_land):
            if not check_bankruptcy(players):
                winner = players[0]
                print(f"{winner['name']} wins the game!")
                break
        current_player_index = (current_player_index + 1) % len(players)

if __name__ == "__main__":
    num_players = int(input("Enter the number of players (2 - 4): "))
    if 2 <= num_players <= 4:
        main(num_players)
    else:
        print("Invalid number of players. Please enter a number between 2 and 4.")